//-------------------------------------------------------------------
//	Copyright (c) 2010-2012 Zhirnov Andrey
//	This file is part of the "UXGEN" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#include "CDebugRender.h"

namespace Engine
{
namespace CoreSystems
{
	
/*
=================================================
	
=================================================
*/
	CDebugRender::CDebugRender(): _bCacheFrameBuffer(false)
	{
	}
	
/*
=================================================
	
=================================================
*/
	CDebugRender::~CDebugRender()
	{
	}
	
/*
=================================================
	
=================================================
*/
	bool CDebugRender::ChackProgramIO()
	{
		if ( !_bCacheFrameBuffer )
		{
			GLFrameBuffer::fb_info_array_t	a_fb_info;
			
			if ( _pCurrentFB == nullptr ) {
				CHECK_RETURN( GLDefaultFrameBuffer::GetAllBuffersInfo( a_fb_info ) );
			}
			else {
				CHECK_RETURN( _pCurrentFB->GetEnabledAttachmentInfo( a_fb_info ) );
			}

			usize j = 0;
			FOR( i, a_fb_info )
			{
				if ( (a_fb_info[i].eObjType != gl_fbo_object::TEXTURE &&
					  a_fb_info[i].eObjType != gl_fbo_object::RENDERBUFFER) ||
					!(a_fb_info[i].eAttachment >= gl_fbo_attach::COLOR_0 &&
					  a_fb_info[i].eAttachment <= gl_fbo_attach::COLOR_0 + UX_GL_MAX_DRAW_BUFFERS) )
				{
					continue;
				}

				if ( j >= _aFrameBufferInput.Count() )
					break;

				_aFrameBufferInput[j].eColorType = a_fb_info[i].eCompType;
				_aFrameBufferInput[j].uLocation	 = a_fb_info[i].eAttachment - gl_fbo_attach::COLOR_0;
				++j;
			}

			Sort( _aFrameBufferInput );

			_bCacheFrameBuffer = true;
		}

		CHECK_VALUE( _aMeshAttribs == _aProgramAttribs );
		CHECK_VALUE( _aFragOutputs == _aFrameBufferInput );
		return true;
	}

//-------------------------------------------------------------------

}	// CoreSystems
}	// Engine